#define _CRT_SECURE_NO_WARNINGS 1
#include"QList.h"

void QueueInit(Queue* q)
{
	assert(q);
	q->_front = NULL;
	q->_rear  = NULL;
}

void QueuePush(Queue* q, QDataType data)
{
	assert(q);
	QNode* newnode = malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("QueuePush malloc fail");
		return;
	}

	newnode->_data = data;
	newnode->_next = NULL;

	if (q->_front == NULL)
	{
		q->_front = q->_rear = newnode;
	}
	else
	{
		q->_rear->_next = newnode;
		q->_rear = newnode;
	}
}

int QueueEmpty(Queue* q)
{
	if (q->_front == NULL)
	{
		return 1;
	}
	return 0;
}

void QueuePop(Queue* q)
{
	assert(q);
    
	if (!QueueEmpty(q))
	{
		QNode* del = q->_front;
		q->_front = q->_front->_next;
	}
	else
		return;
}

QDataType QueueFront(Queue* q)
{
	assert(q);
	if (QueueEmpty(q))
	{
		return -1;
	}
	else
		return q->_front->_data;
}

QDataType QueueBack(Queue* q)
{
	assert(q);
	if (QueueEmpty(q))
	{
		return -1;
	}
	else
		return q->_rear->_data;
}

int QueueSize(Queue* q)
{
	assert(q);
	int size = 0;
	QNode* head = q->_front;
	while (head)
	{
		size++;
		head = head->_next;
	}
	return size;
}

void QueueDestroy(Queue* q)
{
	assert(q);
	while (q->_front)
	{
		QNode* del = q->_front;
		q->_front = q->_front->_next;
		free(del);
    }
	q->_front = NULL;
	q->_rear = NULL;
}